(define-module (luhux services shepherd)
  #:use-module (gnu services)
  #:use-module (gnu packages)
  #:use-module (gnu packages admin)
  #:use-module (gnu services shepherd)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:export (
	    shepherd-nonroot-service-type
	    shepherd-nonroot-configuration
	    )
  )

;;
;;
;;  提供普通用户的shepherd
;;
;;


(define-record-type* <shepherd-nonroot-configuration>
  shepherd-nonroot-configuration make-shepherd-user-configuration
  shepherd-nonroot-configuration?
  (user shepherd-nonroot-configuration-user)
  (group shepherd-nonroot-configuration-group)
  (config shepherd-nonroot-configuration-config)
  (socket shepherd-nonroot-configuration-socket)
  (log shepherd-nonroot-configuration-log)
)


(define shepherd-nonroot-service-type
  (shepherd-service-type
   'shepherd-nonroot
   (lambda (config)
     (define shepherd-nonroot-user (shepherd-nonroot-configuration-user config))
     (define shepherd-nonroot-group (shepherd-nonroot-configuration-group config))
     (define shepherd-nonroot-config (shepherd-nonroot-configuration-config config))
     (define shepherd-nonroot-socket (shepherd-nonroot-configuration-socket config))
     (define shepherd-nonroot-log (shepherd-nonroot-configuration-log config))
     (define shepherd-nonroot-command (list (file-append shepherd "/bin/shepherd")
					    "-c" shepherd-nonroot-config
					    "-s" shepherd-nonroot-socket
					    "-l" shepherd-nonroot-log
					    "-I" ;; socket文件的访问应该由用户决定
					    ))
     (shepherd-service
      (documentation "非特权用户的shepherd")
      (provision (list (symbol-append 'shepherd-nonroot- (string->symbol shepherd-nonroot-user))))
      (start #~(make-forkexec-constructor '#$shepherd-nonroot-command
					  #:user #$shepherd-nonroot-user
					  #:group #$shepherd-nonroot-group
					  ))
      (one-shot? #t) ;; 启动后应该由普通用户接管
      )
     )
   )
  )
